param ( [string] $ConfigFile=$DefaultDatabaseConfigFile
      , [string] $EnvironmentName=$DefaultDatabaseEnv
			, [string] $ServerName=$DefaultServerName
			, [string] $DatabaseName=$DefaultDatabaseName
			, [string] $Username=$DefaultUsername
			, [string] $Password=$DefaultPassword
      , [switch] $Whatif
			, [switch] $Force
			)

. Resolve-DbDeploymentParms

#region $CreateDatabaseQuery
	$CreateDatabaseQuery = @"
USE [master]
GO
CREATE DATABASE [{0}]
GO
EXEC dbo.sp_dbcmptlevel @dbname=N'{0}', @new_cmptlevel=90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [{0}].[dbo].[sp_fulltext_database] @action = 'disable'
end
GO
ALTER DATABASE [{0}] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [{0}] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [{0}] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [{0}] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [{0}] SET ARITHABORT OFF 
GO
ALTER DATABASE [{0}] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [{0}] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [{0}] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [{0}] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [{0}] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [{0}] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [{0}] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [{0}] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [{0}] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [{0}] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [{0}] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [{0}] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [{0}] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [{0}] SET  READ_WRITE 
GO
ALTER DATABASE [{0}] SET RECOVERY FULL 
GO
ALTER DATABASE [{0}] SET  MULTI_USER 
GO
ALTER DATABASE [{0}] SET PAGE_VERIFY CHECKSUM  
GO
USE [{0}]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [{0}] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO
USE [{0}]
GO
"@
#endregion

Drop-Sandbox -EnvironmentName $EnvironmentName -ServerName $ServerName -DatabaseName $DatabaseName -Username $Username -Password $Password -force:$force

$PSSqlConnection = $PSSqlConnectionClass.New($ServerName,"master",$Username,$Password)
try {
	$PSSqlCommand = $PSSqlCommandClass.New($PSSqlConnection)
	
	$PSSqlCommand.Command.CommandType = [System.Data.CommandType]::Text

	Log-Warning "Creating Database $DatabaseName"
	
	$PSSqlCommand.ExecuteNonQuery($CreateDatabaseQuery -f $DatabaseName)

	$PSSqlConnection.Connection.Close()
	if ($DatabaseCreateRolesScript -or $DatabaseCreateUsersScript)
	{
		$PSSqlConnection = $PSSqlConnectionClass.New($ServerName,$DatabaseName)
		$PSSqlCommand = $PSSqlCommandClass.New($PSSqlConnection)
		$PSSqlCommand.Command.CommandType = [System.Data.CommandType]::Text
	}

	if ( $DatabaseCreateRolesScript )
	{
		$script = get-content $DatabaseCreateRolesScript
#		$PSSqlCommand.ExecuteNonQuery($script)
	}
	if ( $DatabaseCreateUsersScript )
	{
		$PSSqlCommand.ExecuteNonQuery("USE $DatabaseName")
		$script = get-content $DatabaseCreateUsersScript
#		$PSSqlCommand.ExecuteNonQuery($script)
	}
} -finally {
	$PSSqlConnection.Connection.Close()
	$PSSqlConnection.Connection.Dispose()
}
